<?php
require_once("../DAO/UserDAO.php");
require_once("../Entities/QuestionResult.php");
require_once("../Entities/Test.php");
require_once("../DAO/HistoryDAO.php");
require_once("../Entities/History.php");
require_once("../Entities/User.php");
require_once("../Utility/WebSession.php");
require_once("../Utility/FunctionUtilities.php");
include("../Chart/libchart.php");

$rightQuestion = 0;
$rightMarks = 0;
$questionResult = WebSession::GetSession("QuestionResult");
$test = WebSession::GetSession("TestSession");
if($test != null)
{
	$totalQuestions = count($test->Details);
	$totalMarks = $test->GetTotalMarks();
	for($i = 0; $i < count($questionResult); $i++)
	{
		if($questionResult[$i]->CheckCorrect() == true)
		{
			$rightQuestion++;
			$rightMarks = $rightMarks + $test->Details[$questionResult[$i]->QuestionIndex]->Marks;
		}
	}
	$chart = new PieChart(800, 350);
	
	$dataSet = new XYDataSet();
	$dataSet->addPoint(new Point("Right Questions", $rightQuestion));
	$dataSet->addPoint(new Point("Wrong Questions", $totalQuestions - $rightQuestion));
	$chart->setDataSet($dataSet);
	
	$chart->setTitle("User Test Result Chart");
	$chart->render("../ChartImages/TestResultQuestionChart.png");
	WebSession::SetSession("QuestionResult", null);
	WebSession::SetSession("TestSession", null);
	WebSession::SetSession("CurrentQuestion", null);
	if(WebSession::CurrentUserNoReturn() != null)
	{
		$history = new History();
		$historyDAO = new HistoryDAO();
		$history->UserId = WebSession::CurrentUserNoReturn()->UserId;
		$history->UserId = 1;
		$history->Marks = $rightMarks;
		$history->TotalMarks = $totalMarks;
		$history->TotalCorrect = $rightQuestion;
		$history->TotalQuestions = $totalQuestions;
		$historyDAO->CreateHistory($history);
	}
	
	$mainContent = "<div class='middleChart'>
						<div class='chartDiv'>
							<img src='../ChartImages/TestResultQuestionChart.png' />
						</div>
						<div class='infoDiv'>
							<ul>
								<li>Right Questions : " . $rightQuestion . " / " . $totalQuestions . "</li>
								<li>Marks : " . $rightMarks . " / " . $totalMarks . "</li>
							</ul>
						</div>
					</div>";
	include("MasterPage.php");
}
else
{
	WebUtilities::Redirect("MainTest.php");
}

?>